package xyz.riceball.usercore.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xyz.riceball.framework.core.annotation.RbLog;
import xyz.riceball.framework.core.annotation.RbSysRequest;
import xyz.riceball.framework.core.bean.ApiController;
import xyz.riceball.framework.core.bean.ApiR;
import xyz.riceball.framework.core.bean.PageParam;
import xyz.riceball.usercore.entity.dto.RoleRightTreeDTO;
import xyz.riceball.usercore.entity.po.RolePO;
import xyz.riceball.usercore.service.RoleService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 角色表(Role)表控制层
 *
 * @author xiaovcloud
 * @since 2022-04-24 20:17:48
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/rb/api/usercore/role")
@RbSysRequest
public class RoleController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 分页查询所有数据
     *
     * @param pageParam 分页对象
     * @param rolePO    查询实体
     * @return 所有数据
     */
    @RbLog(description = "分页查询角色")
    @GetMapping
    @ApiOperation(value = "分页查询角色")
    public ApiR<Page<RolePO>> selectPage(PageParam pageParam, RolePO rolePO) {
        return success(roleService.selectPage(PageParam.<RolePO>of(pageParam), rolePO));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RbLog(description = "根据id查询角色")
    @ApiOperation(value = "根据id查询角色")
    @GetMapping("{id}")
    public ApiR<RolePO> selectOne(@PathVariable Serializable id) {
        return success(roleService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param rolePO 实体对象
     * @return 新增结果
     */
    @RbLog(description = "新增角色")
    @ApiOperation(value = "新增角色")
    @PostMapping
    public ApiR<RolePO> insert(@RequestBody RolePO rolePO) {
        return roleService.add(rolePO);
    }

    /**
     * 修改数据
     *
     * @param rolePO 实体对象
     * @return 修改结果
     */
    @RbLog(description = "修改角色")
    @ApiOperation(value = "修改角色")
    @PutMapping
    public ApiR<Void> update(@RequestBody RolePO rolePO) {
        boolean edit = roleService.edit(rolePO);
        return ApiR.msg(edit, "修改角色成功！", "修改角色失败！");
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @RbLog(description = "删除角色")
    @ApiOperation(value = "删除角色")
    @DeleteMapping
    public ApiR<Void> delete(@RequestBody List<String> ids) {
        return roleService.delete(ids);
    }

    /**
     * 分配功能
     *
     * @param roleRightTreeDTO 功能对象
     * @return
     */
    @RbLog(description = "分配功能")
    @ApiOperation(value = "分配功能")
    @PutMapping("/right")
    public ApiR<Void> putRight(@RequestBody RoleRightTreeDTO roleRightTreeDTO) {
        boolean flag = roleService.putRight(roleRightTreeDTO.getRoleId(), roleRightTreeDTO.getRouteIds());
        return ApiR.msg(flag, "分配功能成功！", "分配功能失败！");
    }

    /**
     * 分配功能
     *
     * @param roleId 角色id
     * @return
     */
    @RbLog(description = "树形查询角色分配的功能")
    @ApiOperation(value = "树形查询角色分配的功能")
    @GetMapping("/right/{roleId}")
    public ApiR<RoleRightTreeDTO> getRight(@PathVariable String roleId) {
        return success(roleService.getRight(roleId));
    }
}

